/**
 * @file lv_watch_list.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_LIST != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_watch_list_ext_t * lv_watch_list_get_ext(lv_obj_t * obj);
static void lv_watch_list_realign(lv_obj_t * obj);
static void lv_watch_list_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * lv_watch_list_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SETTING;
        activity_ext.create = lv_watch_list_create;
        activity_ext.prepare_destory = lv_watch_list_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    lv_watch_obj_ext_t * obj_ext = lv_obj_get_ext_attr(obj);
    if(obj_ext == NULL) return NULL;

    /*Allocate the tab type specific extended data*/
    lv_watch_list_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_watch_list_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    /*ext realloc based on watch_obj_ext, do not memeset it*/
    //memset(ext, 0, sizeof(lv_watch_list_ext_t));

    /*Initialize the allocated 'ext' */
    ext->name = NULL;
    ext->list = NULL;
    ext->list_name_hide = 0;

    /* content */
    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_set_size(content, lv_obj_get_width(obj), lv_obj_get_height(obj));
    lv_cont_set_layout(content, LV_LAYOUT_COLUMN_MID);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_add_style(content,LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label_cont = lv_cont_create(content, NULL);
    lv_obj_set_size(label_cont, lv_obj_get_width(content), 30);
    lv_obj_add_style(label_cont, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_cont_set_layout(label_cont, LV_LAYOUT_OFF);
    lv_cont_set_fit2(label_cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_click(label_cont, false);

    lv_obj_t * label = lv_label_create(label_cont, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text(label, "");
    lv_obj_set_click(label, false);
    ext->name = label_cont;

    lv_obj_t * list = lv_list_create(content, NULL);
    LV_ASSERT_MEM(list);
    if(list == NULL) return NULL;
    ext->list = list;

    lv_watch_obj_add_element(lv_page_get_scrl(list));

    /* update list style*/
    lv_obj_add_style(list,LV_LIST_PART_BG, &lv_watch_style_transp);

    uint8_t pad_ver = 4;
    lv_obj_add_style(list, LV_LIST_PART_SCROLLABLE, &lv_watch_style_transp);
    lv_obj_set_style_local_pad_top(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, pad_ver);
    lv_obj_set_style_local_pad_bottom(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, pad_ver);

    lv_list_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_width(list, lv_obj_get_width(content));
    lv_coord_t height = lv_obj_get_height(content)
                        - lv_obj_get_height(label_cont)
                        - pad_ver * 2;
    lv_obj_set_height(list, height);

    return obj;
}

void lv_watch_list_set_name_hidden(lv_obj_t * obj, bool en)
{
    lv_watch_list_ext_t * ext = lv_watch_list_get_ext(obj);

    ext->list_name_hide = en;
    lv_watch_list_realign(obj);
}

void lv_watch_list_set_name_txt(lv_obj_t * obj, watchLangTxtId_t txtId)
{
    lv_watch_list_ext_t * ext = lv_watch_list_get_ext(obj);
    if(ext->list_name_hide) return;

    if(ext->name) {
        lv_obj_t * label = lv_obj_get_child(ext->name, NULL);
        if(label) lv_label_set_text_id(label, txtId);
        lv_obj_align(label, ext->name, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
    }
}

lv_obj_t * lv_watch_list_add(lv_obj_t * obj, const void * img_src, watchLangTxtId_t txtId, lv_event_cb_t rel_event_cb)
{
    lv_watch_list_ext_t * ext = lv_watch_list_get_ext(obj);
    lv_obj_t * list = ext->list;
    if(list == NULL) return NULL;

    lv_obj_t * btn;

    btn = lv_list_add_btn(list, img_src, lv_lang_get_text(txtId));
    lv_obj_set_event_cb(btn, rel_event_cb);
    lv_watch_obj_add_element(btn);

    lv_obj_set_style_local_pad_top(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 6);
    lv_obj_set_style_local_pad_bottom(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 6);
    lv_obj_set_style_local_pad_left(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_right(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_inner(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 10);

    //re-set long mode of btn label, otherwize the text will scroll
    lv_obj_t *label = lv_obj_get_child(btn, NULL);
    lv_label_set_long_mode(label, LV_LABEL_LONG_EXPAND);
    //set text id of btn label, it will update language when language changed
    lv_label_set_text_id(label, txtId);

    return btn;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_watch_list_ext_t * lv_watch_list_get_ext(lv_obj_t * obj)
{

    lv_watch_list_ext_t  * ext = lv_obj_get_ext_attr(obj);
    return ext;
}

static void lv_watch_list_realign(lv_obj_t * obj)
{
    lv_watch_list_ext_t * ext = lv_watch_list_get_ext(obj);

    if(ext->list_name_hide) {
        lv_obj_set_hidden(ext->name, true);
        lv_coord_t height = lv_obj_get_height(lv_obj_get_parent(ext->list));
        lv_obj_set_height(ext->list, height);
    }
}

static void lv_watch_list_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

#endif /*USE_LV_WATCH_LIST*/
